package com.tech.zhidengxian.service.impl;

import com.tech.zhidengxian.mapper.AttentionMapper;
import com.tech.zhidengxian.model.dataobject.AttentionDO;
import com.tech.zhidengxian.model.dto.AttentionDTO;
import com.tech.zhidengxian.service.api.AttentionService;
import com.tech.zhidengxian.util.JacksonUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Ljh
 * @date 2023/5/14 13:26
 * @description
 **/
@Service
public class AttentionServiceImpl implements AttentionService {
    @Resource
    private AttentionMapper attentionMapper;

    @Override
    public List<AttentionDTO> queryListByUid(Long uid) {
        List<AttentionDO> attentionDOS = attentionMapper.queryListByUid(uid);
        return JacksonUtils.convertList(attentionDOS, AttentionDTO.class);
    }

    @Override
    public AttentionDTO queryAttention(AttentionDTO attentionDTO) {
        AttentionDO attentionDO = attentionMapper.queryByAtAndFans(JacksonUtils.convert(attentionDTO, AttentionDO.class));
        return JacksonUtils.convert(attentionDO,AttentionDTO.class);
    }

    @Override
    public AttentionDTO updateAttention(AttentionDTO attentionDTO) {
        AttentionDO attentionDO = JacksonUtils.convert(attentionDTO, AttentionDO.class);
        AttentionDO query = attentionMapper.queryByAtAndFans(attentionDO);
        if (query != null) {
            query.setIsDeleted(!query.getIsDeleted());
            attentionMapper.updateById(query);
            attentionDTO.setId(query.getId());
        } else {
            attentionMapper.insert(attentionDO);
            attentionDTO.setId(attentionDO.getId());
        }
        return attentionDTO;
    }
}
